package com.joeyzh.soundrecorder.audio;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.joeyzh.soundrecorder.R;
import com.joeyzh.soundrecorder.bean.AudioModel;
import com.joeyzh.soundrecorder.utils.ImageLoaderUtil;

import java.util.List;


public class AddAudioAdapter extends BaseAdapter {


    public static final String EMPTY_PATH = "path://add_image";
    private List<AudioModel> data;
    private Context context;
    //定义接口属性
    private OnItemClickLisener callback;
    private boolean editable;
    public AddAudioAdapter(Context context, List<AudioModel> dataList) {
        this.data = dataList;
        this.context = context;
    }

    public static AudioModel EmptyPath() {
        return new AudioModel(EMPTY_PATH);
    }

    public void setCallback(OnItemClickLisener callback) {
        this.callback = callback;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    @Override
    public AudioModel getItem(int i) {
        return data.get(i);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        convertView = View.inflate(context, R.layout.audio_item_add_record, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);

        AudioModel path = getItem(position);
        setImageView(path, holder);
        return convertView;
    }

    private void setImageView(AudioModel fileBean, ViewHolder holder) {
        //添加音频
        if (EMPTY_PATH.equals(fileBean.getFilePath())) {
            setImageView(holder.img, R.drawable.ic_addpic);
            holder.delImgView.setVisibility(View.INVISIBLE);
            holder.tvDuration.setVisibility(View.GONE);
            return;
        }
        //音频默认显示文本
        setImageView(holder.img, R.drawable.ic_audio);
        //音频时长
        holder.tvDuration.setText(fileBean.getElpasedInt() + "　s");
        holder.tvDuration.setVisibility(View.VISIBLE);

        if (isEditable()) {
            holder.delImgView.setVisibility(View.VISIBLE);
        } else {
            holder.delImgView.setVisibility(View.INVISIBLE);
        }
        final AudioModel finalPath = fileBean;
        holder.delImgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.onDelete(finalPath);
            }
        });
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.onPlay(finalPath);
            }
        });
//        if (ImageLoaderUtil.isBase64Img(finalPath)) {
//            ImageLoaderUtil.loadBase64(context, finalPath, holder.img);
//            return;
//        }
//        Uri uri = Uri.parse(path);
//        if (uri.getScheme() == null || uri.getScheme().isEmpty()) {
//            path = "file://" + path;
//        }
//        ImageLoaderUtil.setImage(holder.img, path);

    }

    public void setText(TextView textView, int res) {
        if (res <= 0) {
            textView.setText("");
            return;
        }
        textView.setVisibility(View.VISIBLE);
        textView.setText(res);
    }


    public void setText(TextView textView, CharSequence text) {
        if (TextUtils.isEmpty(text) || "null".equalsIgnoreCase(text.toString())) {
            textView.setText("");
            return;
        }
        textView.setVisibility(View.VISIBLE);
        textView.setText(text);
    }

    public void setImageView(ImageView imageView, int res) {
        if (res <= 0) {
            return;
        }
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(res);
    }

    public void setImageView(final ImageView imageView, final String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        imageView.setVisibility(View.VISIBLE);
        ImageLoaderUtil.setImage(imageView, url);

    }

    public interface OnItemClickLisener {
        void onDelete(AudioModel fileBean);

        void onPlay(AudioModel fileBean);

    }

    private class ViewHolder {
        ImageView img;
        TextView tvDuration;
        private ImageView delImgView;

        public ViewHolder(View convertView) {
            img = convertView.findViewById(R.id.img_photo);
            delImgView = convertView.findViewById(R.id.img_del);
            tvDuration = convertView.findViewById(R.id.tv_duration);
        }
    }
}